using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.UI;

public class ExcelWin : MonoBehaviour
{
    [SerializeField] private DataItem dataItem;
    [SerializeField] private Button showMapDataBtn;
    [SerializeField] private Text mapDataText;

    private ObjectPool<DataItem> pool;
    private DataItem GetDataItem()
    {
        DataItem item = Instantiate(dataItem, dataItem.transform.parent, false);
        return item;
    }

    // Start is called before the first frame update
    void Start()
    {
        dataItem.Hide();
        pool = new ObjectPool<DataItem>(GetDataItem, item => item.Show(),
            item =>
            {
                item.transform.localScale = Vector3.one;
                item.transform.localPosition = Vector3.zero;
                item.Hide();
            }, item => Object.Destroy(item.gameObject), false, 10, 1000);
        showMapDataBtn.onClick.AddListener(ShowMapData);
    }

    List<DataItem> list = new List<DataItem>();

    private void ShowMapData()
    {
        Restore();
          Map gameMap = ProtoDataCenter.Get<Map>();
          MapItem gameMapItem;
          if (gameMap.DataList.Count > 0)
          {
              gameMapItem = gameMap.DataList[0];
              string dataItemValue = $@"
                                        MapID：{gameMapItem.MapID}
                                        Name：{gameMapItem.Name}
                                        RegionType：{gameMapItem.RegionType}
                                        MonsterDistribution：{gameMapItem.MonsterDistribution}
                                        BackgroundMusic:{gameMapItem.BackgroundMusic}
                                        MapLevel：{gameMapItem.MapLevel}
                                        Location：{gameMapItem.Location}
                                        Size：{gameMapItem.Size}
                                        Description：{gameMapItem.Description}
                                      ";
              mapDataText.text = dataItemValue;
              foreach (var info in MapItem.Descriptor.Fields.InFieldNumberOrder())
              {
                  DataItem item = pool.Get();
                  item.Name = info.Name;
                  item.Value = info.Accessor.GetValue(gameMapItem).ToString();
                  item.transform.SetAsLastSibling();
                  list.Add(item);
              }
        }
    }

    private void Restore()
    {
        foreach (var item in list)
        {
            pool.Release(item);
        }

        list.Clear();
    }

    private void ShowDictAndList()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
    }
}